package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.server.mapper.DriverLoginMapper;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverLogin;
import com.ybkj.daijia.server.service.DriverLoginService;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DefaultDriverLoginService implements DriverLoginService {

    @Autowired
    private DriverLoginMapper driverLoginMapper;

    @Override
    public DriverLogin selectByDriver(Long driverId) {
        return driverLoginMapper.selectByDriver(driverId);
    }

    @Override
    public DriverLogin selectByToken(String token) {
        return driverLoginMapper.selectByToken(token);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(DriverLogin driverLogin) {
        driverLoginMapper.updateTimeByPrimaryKey(driverLogin);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        driverLoginMapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer addPasswordIncorrect(Driver driver) {

        DriverLogin driverLogin = driverLoginMapper.selectByDriver(driver.getId());

        if (null == driverLogin) {
            List<String> params = new LinkedList<String>();
            params.add(driver.getUsername());
            params.add(driver.getPassword());
            params.add(String.valueOf(System.currentTimeMillis()));
            String token = DriverLogin.genToken(params);

            driverLogin = new DriverLogin();
            driverLogin.setDriverId(driver.getId());
            driverLogin.setDriverToken(token);
            Calendar calendar = Calendar.getInstance();
            driverLogin.setRefreshDate(calendar.getTime());
            calendar.add(Calendar.MINUTE, DriverLogin.VALID_TIME);
            driverLogin.setDeathDate(calendar.getTime());
            driverLoginMapper.insertRecord(driverLogin);
        }

        driverLoginMapper.addPasswordIncorrect(driverLogin.getId());

        return driverLogin.getPassError() + 1;

    }

    @Transactional(rollbackFor = Exception.class)
    public Integer addCodeIncorrect(Driver driver) {

        DriverLogin driverLogin = driverLoginMapper.selectByDriver(driver.getId());

        if (null == driverLogin) {
            List<String> params = new LinkedList<String>();
            params.add(driver.getUsername());
            params.add(driver.getPassword());
            params.add(String.valueOf(System.currentTimeMillis()));
            String token = DriverLogin.genToken(params);

            driverLogin = new DriverLogin();
            driverLogin.setDriverId(driver.getId());
            driverLogin.setDriverToken(token);
            Calendar calendar = Calendar.getInstance();
            driverLogin.setRefreshDate(calendar.getTime());
            calendar.add(Calendar.MINUTE, DriverLogin.VALID_TIME);
            driverLogin.setDeathDate(calendar.getTime());
            driverLoginMapper.insertRecord(driverLogin);
        }

        driverLoginMapper.addCodeIncorrect(driverLogin.getId());

        return driverLogin.getCodeError() + 1;

    }

}
